package com.webshop.DataBase;

import com.webshop.JavaBean.Category;
import com.webshop.JavaBean.Goods;
import com.webshop.Utils.ServletOut;
import com.webshop.service.ControlProductService;
import net.sf.json.JSON;
import net.sf.json.JSONArray;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/adminservlet")
public class AdmainServlet extends javax.servlet.http.HttpServlet{
    ControlProductService con=new ControlProductService();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        String method=req.getParameter("method");
        switch (method){
            case "show":
                show(req,resp);
                break;
            case "off":
                up(req,resp);
                break;
            case "on":
                on(req,resp);
                break;
            default:
                break;
        }
    }

    /**
     * 分页返回商品列表在首页显示
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void show(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session=req.getSession();
        Object obj = session.getAttribute("admainname");
        String name = String.valueOf(obj);
        if (obj == null) {
            ServletOut.request(req,resp,"/html/admin_login.jsp");
        }
        String p=req.getParameter("page");
        System.out.println(p);
        int page=Integer.parseInt(p);
        System.out.println(page);
        int num=5;
        List<Goods> goods=new ArrayList<>();
        List<Category> categories=new ArrayList<>();
        categories=con.show(0);
        goods=con.findgood(name,page,num);
        req.setAttribute("goods",goods);
        req.setAttribute("page", page);
        System.out.println(goods.toString());
        //req.setAttribute("category",categories);
        ServletOut.request(req,resp,"/html/productManager.jsp");
    }

    /**
     * 下架商品
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
  public void up(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session=req.getSession();
       Object obj = session.getAttribute("admainname");
          String name = String.valueOf(obj);
             String json=req.getParameter("jsondata");
             String temp = req.getParameter("page");
             System.out.println(json);


             JSONArray jsonArray=JSONArray.fromObject(json);
             for(int i=0;i<jsonArray.size();i++){
                 String ID=(String.valueOf(jsonArray.getJSONObject(i).getInt("ID")));
                 con.upoffProduct("0",ID);
             }
      int page = Integer.parseInt(temp);
          List<Goods>  goods=con.findgood(name,page,5);
         req.setAttribute("goods",goods);
          req.setAttribute("page", page);
             ServletOut.request(req,resp,"/html/productManager.jsp");

  }

    /**
     * 上架商品
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void on(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String joson=req.getParameter("jsonon");
        String page = req.getParameter("page");
        System.out.println(joson);
        System.out.println(page);
        /**JSONArray jsonArray=JSONArray.fromObject(joson);
        for(int i=0;i<jsonArray.size();i++){
            String ID=(String.valueOf(jsonArray.getJSONObject(i).getInt("ID")));
            String num=(String.valueOf(jsonArray.getJSONObject(i).getInt("num")));
            con.upoffProduct("1",num);
        }
        ServletOut.request(req,resp,"/html/admin.jsp");*/
    }
}
